//index.js
//获取应用实例
const config = require('../../config/api.js');
const app = getApp()


Page({
  data: {
    changeStyle: false,
    proinfo: [],
    bannerList: [],
    buildingList: [],
    taskList: [],
    currentTab: 0,
    scrollLeft: 0,
    agreeModal: false,
    certModal: false,
    maskmodel: false,
    integralModal: false,
    isChecked: true
  },
  onShow: function() {
    let bannerListUrl = config.bannerListUrl;
    let buildingUrl = config.buildingUrl;
    let taskListUrl = config.taskListUrl;
    let that = this;
    app.getToken().then(res => {
        wx.request({
          url: 'https://api.mokyun.com/express/login',
          method: 'POST',
          data: {
            nickName: res.userInfo.nickName,
            headImgUrl: res.userInfo.avatarUrl
          },
          header: {
            'Authorization': 'Bearer ' + wx.getStorageSync('token')
          },
          success(res) {
            that.http(bannerListUrl, that.bannerCallback);
            that.http(buildingUrl, that.buildingCallback);
            wx.request({
              url: config.taskListUrl,
              method: 'GET',
              header: {
                'Authorization': 'Bearer ' + wx.getStorageSync('token')
              },
              success: result => {
                if(result.data.result=='empty!'){
                  that.setData({
                    proinfo: ''
                  })
                  return
                }
                that.setData({
                  proinfo:''
                })
                let buildingList = that.data.buildingList;
                let allTask = result.data;
                let allArr = [];
                for(let i = 0,len = allTask.length;i<len;i++){
                  console.log(allTask[i])
                }
                // let someTask = allTask.filter((val,index)=>{
                //   if(val.building_id==buildingList[index].id){
                //     return val
                //   }
                // })
                // console.log(someTask)
                console.log(allArr)
                console.log(result.data)
                console.log(buildingList)
                // that.setData({
                //   allTask:result.data
                // })
                // let res = result.data;
                // console.log(res)
                // that.setDatas(buildingList[0].id,res,that)
                // let newData = res.filter((val, index) => {
                //   if (val.building_id == buildingList[0].id) {
                //     console.log(val)
                //     return val
                //   }
                // })
                // console.log(newData)
                // that.setData({
                //   proinfo: newData
                // })
                // console.log(that.data.proinfo)
              }
            })
          },
          fail(e) {

          }
        })
      })
      .then(res => {
        // console.log(res)
        // that.http(bannerListUrl, that.bannerCallback);
        // that.http(buildingUrl, that.buildingCallback);
        // that.taskListCallback().then(res => {
        //   // console.log(res)
        //   // if(res.result=='empty!'){
        //   //   return
        //   // }
        //   let newData = res.filter((val, index) => {
        //     if (val.building_id == 8) {
        //       return val
        //     }
        //   })
        //   that.setData({
        //     proinfo: newData
        //   })
        // })
        if (getApp().globalData.first === "1") {
          that.setData({
            maskModal: true,
            agreeModal: true,
            integralModal: true
          })
        }
      })
      .catch(e => {
        wx.reLaunch({
          url: '/pages/authorization/authorization',
        })
      })

  },

  http: function(url, callback) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: url,
        method: 'GET',
        header: {
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        success: result => {
          callback(result.data)
        }
      })
    }).catch(e => {
      reject()
    })

  },
  //banner回调函数
  bannerCallback: function(res) {
    for (let i = 0; i < res.length; i++) {
      res[i].img_url = 'https://cdn1-open.mokyun.com/' + res[i].img_url;
    }
    this.setData({
      bannerList: res
    })
  },

  //building回调函数
  buildingCallback: function(res) {
    res.forEach((val, index) => {
      // console.log(val)
      res[index].index = index
    })
    this.setData({
      buildingList: res
    })
  },
  //taskList回调函数
  taskListCallback() {
    return new Promise((resolve, reject) => {
      wx.request({
        url: config.taskListUrl,
        method: 'GET',
        header: {
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        success: result => {
          // console.log(result)
          resolve(result.data)
        }
      })
    }).catch(e => {
      reject()
    })
  },


  // 楼切换函数
  swiperTab: function(e) {
    console.log(e)
    var that = this;
    that.data.buildingList.forEach((v, index) => {
      if (e.detail.current == v.index) {
        // console.log(v)
        // that.setDatas(v.id,that.data.allTask,that)
        // let newData = that.data.allTask.filter((val, index) => {
        //   // console.log(val)
        //   if (val.building_id == v.id) {
        //     return val
        //   }
        // })
        // console.log(newData)
        // that.setData({
        //   proinfo: newData
        // })

      
      }
    })
    that.setData({
      currentTab: e.detail.current
    });
  },
  checkCor: function() {
    if (this.data.currentTab > 5) {
      this.setData({
        scrollLeft: 20
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  styleChange: function() {
    this.setData({
      changeStyle: true
    })
  },
  scrolltoupper: function() {
    this.setData({
      changeStyle: false
    })
  },
  // 楼点击切换
  clickTab: function(e) {
    // console.log(e)
    var that = this;
    let res = that.data.allTask;
    if (that.data.currentTab == e.currentTarget.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.currentTarget.dataset.current
      });
    }
    // if (res) {
    // that.setDatas(e.currentTarget.dataset.id,res,that)
      // return
    // }
      // let newData = that.data.allTask.filter((val, index) => {
      //   if (val.building_id == e.currentTarget.dataset.id) {
      //     return val
      //   }
      // })
      // that.setData({
      //   proinfo: newData
      // })

  },
  //  数据渲染
  setDatas(id,res,_that) {
    // this.taskListCallback().then(res => {
      if(!res){
        return
      }
      // let newData = res.filter((val, index) => {
      //   if (val.building_id == id) {
      //     return val
      //   }
      // })
      // _that.setData({
      //   proinfo: newData
      // })
  // }
  },

  goTaskDetails(e) {
    var taskid = e.currentTarget.dataset.taskid;
    wx.navigateTo({
      url: '/pages/task/task-somedetails/task-somedetails?taskid=' + taskid,
    })
  },

  agree() {
    this.setData({
      agreeModal: false,
    })
    if (this.data.agreeModal === false && this.data.integralModal === false) {
      this.setData({
        maskModal: false
      })
    }
  },

  confirm() {
    this.setData({
      integralModal: false
    });
    if (this.data.agreeModal === false && this.data.integralModal === false) {
      this.setData({
        maskModal: false
      })
    }
  },

  checkboxChange(e) {
    if (e.detail.value.length === 1) {
      this.setData({
        isChecked: false
      })
    } else {
      this.setData({
        isChecked: true
      })
    }
  },
  goTask: function() {
    wx.switchTab({
      url: '/pages/task/task'
    })
  }
})